Custom AuthenticationFailureHandler এবং AccessDeniedHandler ব্যবহার

Java Technologies - স্প্রিং সিকিউরিটি (Spring Security) - Spring Security এর Exception Handling
201

Spring Security তে AuthenticationFailureHandler এবং AccessDeniedHandler হল দুটি গুরুত্বপূর্ণ ইন্টারফেস, যা প্রমাণীকরণ ব্যর্থ হলে বা ব্যবহারকারী অনুমোদিত না হলে কাস্টম হ্যান্ডলিং করতে সাহায্য করে। এগুলি সাধারণত ব্যবহারকারীকে আরও ভাল ফলাফল এবং তথ্য প্রদান করার জন্য কাস্টমাইজ করা হয়।


AuthenticationFailureHandler:

AuthenticationFailureHandler হল একটি ইন্টারফেস যা AuthenticationException ফেলা হলে প্রক্রিয়া পরিচালনা করে। এটি সাধারণত লগইন প্রক্রিয়ার সময় ব্যবহার করা হয়, যেমন ব্যবহারকারী ভুল পাসওয়ার্ড বা ইউজারনেম প্রদান করলে।

AuthenticationFailureHandler এর উদাহরণ:

import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {
        
        // এখানে কাস্টম ফেইল মেসেজ প্রদর্শন করা যেতে পারে
        String errorMessage = "Invalid username or password. Please try again.";
        
        // ফেইল মেসেজ কাস্টম রিডিরেক্টে পাঠানো
        request.setAttribute("error", errorMessage);
        request.getRequestDispatcher("/login?error").forward(request, response);
    }
}

ব্যাখ্যা:

  • এই হ্যান্ডলারটি onAuthenticationFailure মেথডটি প্রদান করে যা ব্যবহারকারী যখন ভুল ইউজারনেম বা পাসওয়ার্ড দেয় তখন ট্রিগার হয়।
  • এর মধ্যে request.setAttribute ব্যবহার করে একটি কাস্টম ত্রুটি বার্তা সেট করা হয় এবং তারপর ব্যবহারকারীকে /login?error পেজে রিডিরেক্ট করা হয়।

Spring Security Configuration এ Custom AuthenticationFailureHandler ব্যবহার:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginPage("/login")
                .failureHandler(customAuthenticationFailureHandler)  // Custom Failure Handler
                .permitAll();
    }
}

AccessDeniedHandler:

AccessDeniedHandler হল একটি ইন্টারফেস যা ব্যবহারকারী যদি অনুমোদনপ্রাপ্ত না হন, যেমন তারা একটি সীমাবদ্ধ রিসোর্স অ্যাক্সেস করতে চায় তবে এটি ট্রিগার হয়। সাধারণত এটি 403 Forbidden এর জন্য কাস্টম হ্যান্ডলিং বা রিডিরেক্ট ব্যবহৃত হয়।

AccessDeniedHandler এর উদাহরণ:

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
            throws IOException, ServletException {
        
        // কাস্টম ত্রুটি বার্তা ব্যবহারকারীকে প্রদর্শন করা
        String errorMessage = "You do not have permission to access this page.";
        
        // রিডিরেক্ট পেজে ত্রুটি বার্তা পাঠানো
        request.setAttribute("error", errorMessage);
        request.getRequestDispatcher("/access-denied").forward(request, response);
    }
}

ব্যাখ্যা:

  • যখন ব্যবহারকারী একটি সীমাবদ্ধ রিসোর্স অ্যাক্সেস করার চেষ্টা করে এবং তাদের অনুমতি না থাকে, তখন AccessDeniedHandler এর handle মেথড কল হবে।
  • এই উদাহরণে, একটি কাস্টম ত্রুটি বার্তা request.setAttribute ব্যবহার করে সেট করা হয়েছে এবং /access-denied পেজে রিডিরেক্ট করা হয়েছে।

Spring Security Configuration এ Custom AccessDeniedHandler ব্যবহার:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")  // ADMIN রোল ছাড়া অ্যাক্সেস নিষিদ্ধ
                .anyRequest().authenticated()
            .and()
            .exceptionHandling()
                .accessDeniedHandler(customAccessDeniedHandler);  // Custom Access Denied Handler
    }
}

Full Example:

এখন আমরা পুরো প্রক্রিয়াটি একত্রিত করি: একটি কাস্টম AuthenticationFailureHandler এবং AccessDeniedHandler সহ Spring Security কনফিগারেশন।

Custom AuthenticationFailureHandler এবং AccessDeniedHandler সহ Spring Security Configuration:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationFailureHandler customAuthenticationFailureHandler;

    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginPage("/login")
                .failureHandler(customAuthenticationFailureHandler)  // Custom Authentication Failure Handler
                .permitAll()
            .and()
            .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")  // ADMIN রোল ছাড়া অ্যাক্সেস নিষিদ্ধ
                .anyRequest().authenticated()
            .and()
            .exceptionHandling()
                .accessDeniedHandler(customAccessDeniedHandler)  // Custom Access Denied Handler
            .and()
            .csrf().disable();  // CSRF Disabling (Optional)
    }
}

শেষ কথা:

  • AuthenticationFailureHandler এবং AccessDeniedHandler হল Spring Security তে কাস্টম প্রক্রিয়া পরিচালনার জন্য গুরুত্বপূর্ণ উপাদান। এগুলি কাস্টম ফেইলিউর এবং অ্যানথোরাইজেশন হ্যান্ডলিংয়ের মাধ্যমে ব্যবহারকারীর অভিজ্ঞতা উন্নত করতে সাহায্য করে।
  • এগুলি Custom Error Messages, Custom Redirects, Logging, এবং অন্যান্য কাস্টম কাজের জন্য ব্যবহার করা যেতে পারে।
  • এগুলি ব্যবহার করে আপনি আপনার নিরাপত্তা ব্যবস্থা আরও শক্তিশালী এবং ব্যবহারকারী বান্ধব করতে পারেন।
Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...